cap program drop mystringsort 
program mystringsort 
	version 8 
	gettoken varname 0 : 0 
	capture confirm string var `varname' 
	if _rc { 
		di as err "`varname' should be a string variable" 
		exit 198 
	} 	

	tempvar encoded 
	tempname lblname
	local i = 1 
	
	foreach w of local 0 { 
		label def `lblname' `i++' `"`w'"', modify 	
	} 

	encode `varname', gen(`encoded') label(`lblname') 
	sort `encoded' 
end 	


cd "$replication\code\"

set matsize 5000

****************************************************************************** Input variables ************************************************************
global cluster "sd_code"
global AS_infs `""""'
global AS_var "NA"
global sample "year!=."
global controls ""
global controlsXYFEs ""
global controlsXinfs ""
global omitted_year1=-1
global omitted_year2=-6
global min_year=-6
global max_year=5
global colour="no_graphs"
global depvar "icw_irrigation"

global int "elecXroad"
local inf1=substr("${int}",1,4)
local inf2=substr("${int}",6,.)
global  infs "`inf1' `inf2' ${int}"
global absorb "shrid_code i.s_code#i.year"

qui : do Z_Event_code.do
estimates restore post_elecXroad
global b_baseline=e(b)[1,1]
global V_baseline=e(V)[1,1]

global infsXcontrols "elec road"
global control_list  "coalition_2004 year_elc_2004 pc01_vd_p_sch_d pc01_vd_m_sch_d pc01_vd_s_sch_d pc01_vd_s_s_sch_d pc01_vd_college_d ln_pc01_pca_no_hh ln_pc01_pca_tot_p pc01_pca_p_sc_share pc01_pca_p_st_share pc01_pca_p_lit_share pc01_vd_power_agr pc01_vd_power_dom pc01_vd_power_all pc01_vd_dirt_road pc01_vd_tar_road ln_pc01_vd_area  ln_tdist_10 ln_tdist_50 ln_tdist_100 ln_tdist_500 tot_irr_share_2001  h_cntr_1_2001_d ph_cntr_1_2001_d drnk_wat_f_1_2001_d post_off_1_2001_d phone_1_2001_d"


foreach var in $control_list {
	global controls ""
	global controlsXinfs "`var'"
	global title "`var''"
	qui : do Z_Event_code.do
	estimates restore post_elecXroad
	global b_`var'=e(b)[1,1]
	global V_`var'=e(V)[1,1]
}


clear
local controls=wordcount("${control_list}")
local obs=`controls'+1
set obs `obs'

gen var=""
gen b=.
gen V=.
local i=1
replace var="baseline" if _n==`i'
replace b=$b_baseline if _n==`i'
replace V=$V_baseline if _n==`i'

foreach var in $control_list {
    local i=`i'+1
	replace var="`var'" if _n==`i'
	replace b=${b_`var'} if _n==`i'
	replace V=${V_`var'} if _n==`i'
}


gen ub=b+1.96*sqrt(V)
gen lb=b-1.96*sqrt(V)
drop if  var=="baseline" | var=="pc01_vd_tar_road"

global rename_list `" coalition_2004="MLA party aligns w/ state" year_elc_2004="MLA election year" ln_pc01_pca_tot_p="Log population" ln_pc01_pca_no_hh="Log households" ln_pc01_vd_area="Log area" tot_irr_share_2001="Share irrigated"  pc01_vd_power_all="Electrified"  pc01_vd_power_agr="Agricutural electricity" pc01_vd_power_dom="Domestic electricity"  pc01_vd_dirt_road="Dirt road" pc01_vd_p_sch_d="Primary school" pc01_vd_m_sch_d="Middle school" pc01_vd_s_sch_d="Secondary school" pc01_vd_s_s_sch_d="Senior secondary sch" pc01_vd_college_d="College"  h_cntr_1_2001_d="Health centre" ph_cntr_1_2001_d="Primary health centre" drnk_wat_f_1_2001_d="Drinking water" post_off_1_2001_d="Post office" phone_1_2001_d="Landline phone"  pc01_pca_p_sc_share="Scheduled caste share" pc01_pca_p_st_share="Scheduled tribes share" pc01_pca_p_lit_share="Literate share"  "'
* 



foreach d in 10 50 100 500 {
		global rename_list `"${rename_list} ln_tdist_`d'="Log km to town of `d',000" "'
}

mystringsort var coalition_2004 year_elc_2004 ln_pc01_pca_tot_p ln_pc01_pca_no_hh ln_pc01_vd_area  pc01_pca_p_sc_share pc01_pca_p_st_share pc01_pca_p_lit_share tot_irr_share_2001  pc01_vd_power_all pc01_vd_power_agr pc01_vd_power_dom  pc01_vd_dirt_road pc01_vd_p_sch_d pc01_vd_m_sch_d pc01_vd_s_sch_d pc01_vd_s_s_sch_d pc01_vd_college_d  h_cntr_1_2001_d ph_cntr_1_2001_d drnk_wat_f_1_2001_d post_off_1_2001_d phone_1_2001_d ln_tdist_10 ln_tdist_50 ln_tdist_100 ln_tdist_500 
 
mkmat b lb ub, rownames(var) matrix(A)


matrix AT=A'
local colour purple
coefplot matrix(AT), ci((2 3)) xscale(range(0 (.01) .12)) xlabel(0 (.02) .12) xtitle(" " "Coefficient on Electricity and Road interaction") rename($rename_list)  graphregion(color(white))  mcolor(`colour'*1.5) ciopts(lcolor(`colour'*2))

graph export "..\Output\het_rob.png", replace